package com.fit.pages;

import java.util.Collection;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.BorrowBean;
import com.fit.interfaces.Borrow;
import com.fit.interfaces.Car;
import com.fit.interfaces.Driver;
import com.fit.interfaces.Employer;
import com.fit.interfaces.User;
import com.fit.service.Borrows;
import com.fit.service.Cars;
import com.fit.service.Drivers;
import com.fit.service.Employers;

public class EntryBorrow {

	@SessionState
	private User user;
	
	private boolean userExists;

	public User getUser() {
		return user;
	}

	Object onActivate(){
		if(!userExists) return Login.class;
		return null;
	}
	
	@Persist
	private String borrowDriver;
	
	@Persist
	private String borrowEmployer;
	
	@Persist
	private String borrowCar;
	
	//geteri za persist promenljive
	
	public String getBorrowDriver() {
		return borrowDriver;
	}

	public void setBorrowDriver(String borrowDriver) {
		this.borrowDriver = borrowDriver;
	}

	public String getBorrowEmployer() {
		return borrowEmployer;
	}

	public void setBorrowEmployer(String borrowEmployer) {
		this.borrowEmployer = borrowEmployer;
	}

	public String getBorrowCar() {
		return borrowCar;
	}

	public void setBorrowCar(String borrowCar) {
		this.borrowCar = borrowCar;
	}

	@Inject
	private Cars cars;
	
	@Inject
	private Drivers drivers;
	
	@Inject
	private Employers employers;
	
	@Inject
	private Borrows borrows;
	
	public Collection<Car> getCars(){
		return cars.retrieve();
	}
	
	public Collection<Driver> getDrivers(){
		return drivers.retrive();
	}
	
	public Collection<Employer> getEmployers(){
		return employers.retrive();
	}
	
	@InjectPage
	private ViewBorrow viewBorrowPage;
	
	@InjectPage
	private ViewError viewErrorPage;
	
	@OnEvent (value="submit" ,component="borrowInputForm")
	Object onSubmit(){
		borrowEmployer = "WebApp";
		borrowDriver = getUser().getUserName();
		
		Car car = null;
		Driver driver = null;
		Employer employer = null;
		
		try {
			driver = drivers.retriveSingleDriver(getBorrowDriver());
			employer = employers.retriveSingleEmployer(getBorrowEmployer());
			car = cars.retriveSingleCar(getBorrowCar());
		} catch (Exception e) {
			// TODO: handle exception
			viewErrorPage.setPassedMessage(e.toString());
			return viewErrorPage;
		}
		if(driver == null){
			viewErrorPage.setPassedMessage("Error in recognition object Drivers in Map's");
			return viewErrorPage;
		}
		if(employer == null){
			viewErrorPage.setPassedMessage("Error in recognition object Employers in Map's");
			return viewErrorPage;
		}
		if(car == null){
			viewErrorPage.setPassedMessage("Error in recognition object Cars in Map's");
			return viewErrorPage;
		}
		Borrow borrowBean = new BorrowBean();
		borrowBean.executeBorrowCreation(driver, employer, car, borrows);
		this.borrowCar = "";
		this.borrowDriver = "";
		this.borrowEmployer = "";
		
		return viewBorrowPage;
	}
}
